package cn.zhf.shop.service.impl;

import cn.zhf.shop.entity.CreditApply;
import cn.zhf.shop.entity.Message;
import cn.zhf.shop.entity.search.CreditApplySearch;
import cn.zhf.shop.mapper.CreditApplyMapper;
import cn.zhf.shop.service.CreditApplyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CreditApplyServiceImpl implements CreditApplyService {

    @Resource
    private CreditApplyMapper creditApplyMapper;


    @Override
    public Message<?> insert(CreditApply c) {
        c.setCreateTime(new Date());
        c.setResult(0);
        List<CreditApply> creditApplies = creditApplyMapper.selectByBankNameAndUserIdcard(c.getBankName(),c.getUserIdcard());
        if (creditApplies.size()!=0)
        {


            CreditApply creditApply = creditApplies.get(creditApplies.size()-1);
            if (creditApply.getResult()==0)
            {
                return Message.success(null);
            }else if (creditApply.getResult()==-1)
            {
                return creditApplyMapper.insert(c) > 0 ? Message.success(null) : Message.fail(null);
            }else if (creditApply.getResult()==1)
            {
                return creditApplyMapper.insert(c) > 0 ? Message.success(null) : Message.fail(null);
            }
        }

        return creditApplyMapper.insert(c) > 0 ? Message.success(null) : Message.fail(null);



    }

    @Override
    public Message<CreditApply> getById(Integer id) {
        CreditApply creditApply = creditApplyMapper.selectByPrimaryKey(id);
        return creditApply == null ? Message.fail("未找到") : Message.success(null);
    }

    @Override
    public Message<List<CreditApply>> getAll() {
        List<CreditApply> creditApplies = creditApplyMapper.selectAll();
        return Message.success(null).add(creditApplies);
    }

    @Override
    public Message<?> updateStatusById(Integer id, Integer status) {
        return creditApplyMapper.updateResultById(status, id) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<List<CreditApply>> selectByPhone(String phone) {
        return Message.success(null).add(creditApplyMapper.selectByPhone(phone));
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return creditApplyMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(CreditApply record) {
        return creditApplyMapper.insertSelective(record);
    }

    @Override
    public CreditApply selectByPrimaryKey(Integer id) {
        return creditApplyMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(CreditApply record) {
        return creditApplyMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CreditApply record) {
        return creditApplyMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message<List<CreditApply>> selectByIdAndBankNameAndUserNameAndPhoneAndResultAndUserIdcard2(Integer id, String bankName, String userName, String phone, Integer result, String userIdcard) {
        return Message.success(null).add(creditApplyMapper.selectByIdAndBankNameAndUserNameAndPhoneAndResultAndUserIdcard2(id, bankName, userName, phone, result, userIdcard));
    }

    @Override
    public Message<List<CreditApply>> selectByIdAndBankNameAndUserNameAndPhoneAndResultAndUserIdcard(String str) {
        if (str.equals("待处理"))
        {
            return Message.success(null).add(creditApplyMapper.selectAllByResult(0));

        }else if (str.equals("已成功"))
        {
            return Message.success(null).add(creditApplyMapper.selectAllByResult(1));

        }else if (str.equals("已回绝"))
        {
            return Message.success(null).add(creditApplyMapper.selectAllByResult(-1));
        }
        return Message.success(null).add(creditApplyMapper.selectByIdAndBankNameAndUserNameAndPhoneAndUserIdcard(str));
    }

    @Override
    public Message mohuConditionSearch(CreditApplySearch c) {
        return Message.success(null).add(creditApplyMapper.selectByUserIdcardAndResultAndPhoneAndBankNameAndUserName(c));
    }
}

